const express=require('express')
const router= express.Router();
const db=require('../../utils/db')

// 查询桌位区域
router.get('/gettablearea',function(req,res){
    let mysql=`SELECT table_position FROM table_area GROUP BY table_position`
    db(mysql,null,function(err,data){
        if(err){
            return res.json({
                code: 500,
                msg: '查询桌位区域失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询桌位区域成功',
            data: data
        })
    })
})

// 查询桌位数据
router.get('/gettablelist',function(req,res){
    let mysql=`SELECT * FROM table_area WHERE table_position=?`
    db(mysql,[req.query.area],function(err,data){
        if(err){
            return res.json({
                code: 500,
                msg: '查询桌位列表失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询桌位列表成功',
            data: data
        })
    })
})

// 添加桌位数据
router.post('/addtablelist',function(req,res){
    console.log(req.body);
    console.log(req.body.table_position[0]);
    let mysql='INSERT INTO table_area VALUE(NULL,?,?,?,?,?)'
    db(mysql,[req.body.table_position,req.body.table_contain,req.body.table_state,req.body.table_num,req.body.table_size],(err,data)=>{
        if(err){
            return res.json({
                code: 500,
                msg: '添加桌位失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '添加桌位成功',
            // data: data
        })
    })
})

module.exports = router